package com.wise.service.portal.content.articles;

import com.wise.entities.content.Article;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoList;
import com.wise.summer.v1.portal.content.articles.SearchArticles;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.Arrays;
import java.util.List;

/**
 * 搜索新闻
 *
 * @author Haoming Chen
 * Created on 2019/11/18
 */
@Service
@SummerService
public class SearchArticlesService implements SummerServiceBean<SearchArticles> {

    @Override
    public void sum(SearchArticles summer) {
        SqlSelect sqlSelect = SqlSelect.selec("*")
                .fromClassName(Article.class)
                .eqLang(Article._lang)
                .eq(Article._state, "1")
                .eq(StringUtils.isNotBlank(summer.getColumnId()), Article._columnId, summer.getColumnId());
        if (StringUtils.isNotBlank(summer.getTitle())) {
            //分词
            List<String> terms = Arrays.asList(StringUtils.split(summer.getTitle(), " "));
            for (int i = 0; i < terms.size(); i++) {
                sqlSelect.like(Article._title, "%" + terms.get(i) + "%");
                if (terms.size() - i > 1) {
                    sqlSelect.or();
                }
            }

//            List<Term> terms = AnsjUtil.getAnsj(summer.getTitle());
//            for (int i = 0; i < terms.size(); i++) {
//                sqlSelect.like(Article._title, "%" + terms.get(i).getName() + "%");
//                if (terms.size() - i > 1) {
//                    sqlSelect.or();
//                }
//            }
        }
        sqlSelect.order_by(Article._sort)
                .order_by(Article._publishTime);

        List<Article> resultList = DaoList.s(Article.class, sqlSelect);

        summer.setSummerResult(resultList);
    }
}
